package com.yuebaix.pangu.ready;

import com.yuebaix.pangu.core.PanGuCoreConst;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.annotation.PostConstruct;

@ConditionalOnProperty(
        value = PanGuReadyStarterConst.PAN_GU_READY_STARTER_SWAGGER_ENABLED,
        havingValue = "true",
        matchIfMissing = true
)
@EnableOpenApi
@Slf4j
public class SwaggerAutoConfiguration {
    @PostConstruct
    public void postInit() {
        log.info(PanGuCoreConst.PAN_GU_TRACE_PREFIX + "PanGu Ready Swagger Initialized");
    }

    @Bean("DefaultDocket")
    @ConditionalOnMissingBean(Docket.class)
    public Docket defaultDocket() {
        return new Docket(DocumentationType.OAS_30)
                .groupName("default")
                .apiInfo(apiInfo())
                .useDefaultResponseMessages(false)
                //.pathMapping(socialGraphServiceProperties.getContextPath())
                .select()
                //扫描注解了@ApiOperation的方法生成API接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("ApiDoc")
                .contact(new Contact(PanGuCoreConst.AUTHOR_NAME, PanGuCoreConst.AUTHOR_GITHUB, PanGuCoreConst.AUTHOR_EMAIL))
                .version(PanGuReadyStarterConst.PAN_GU_READY_STARTER_VERSION)
                .description("ApiDoc generated by pangu-ready-spring-boot-starter")
                .build();
    }
}
